/**
 * 健康检查相关 API
 */

import { get } from './request'
import { API_ENDPOINTS } from './config'

// 健康检查响应接口（与后端接口保持一致）
export interface HealthResponse {
  status: string
  message: string
  timestamp: string
  version: string
  database: string
  environment: string
}

/**
 * 健康检查
 * 用于检测 API 服务是否正常运行
 */
export const checkHealth = (): Promise<HealthResponse> => {
  return get<HealthResponse>(API_ENDPOINTS.HEALTH)
}

/**
 * 测试 API 连接
 * 包含错误处理的健康检查
 */
export const testApiConnection = async (): Promise<{
  success: boolean
  message: string
  data?: HealthResponse
}> => {
  try {
    const response = await checkHealth()
    return {
      success: true,
      message: 'API连接正常',
      data: response
    }
  } catch (error: any) {
    return {
      success: false,
      message: error.message || 'API连接失败'
    }
  }
}

/**
 * 快速健康检查
 * 只返回服务是否健康的布尔值
 */
export const isApiHealthy = async (): Promise<boolean> => {
  try {
    const response = await checkHealth()
    return response.status === 'healthy'
  } catch (error) {
    return false
  }
}

/**
 * 获取服务器信息
 * 返回服务器的基本信息
 */
export const getServerInfo = async (): Promise<{
  version: string
  environment: string
  database: string
  uptime?: string
} | null> => {
  try {
    const response = await checkHealth()
    return {
      version: response.version,
      environment: response.environment,
      database: response.database,
      uptime: response.timestamp
    }
  } catch (error) {
    console.error('获取服务器信息失败:', error)
    return null
  }
}
export const getHealthTest = async (): Promise<HealthResponse> => {
  const params = {
    param: 'test'
  }
  return get<HealthResponse>(API_ENDPOINTS.HEALTH_TEST, params)
}
